package com.parktech.sun.web.controller.management.industryservice;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.industryservice.IndustryService;
import com.parktech.sun.domain.model.industryservice.IndustryServiceInfoCategory;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.industryservice.IndustryServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;

/**
 * User: Think
 * Date: 11-1-17
 * Time: 下午7:33
 */
@Controller
@RequestMapping("/management/industryservice/{industryServiceId}/categories")
public class IndustryServiceInfoCategoryController {

    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private IndustryServiceService industryServiceService;

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Autowired
    public IndustryServiceInfoCategoryController(IndustryServiceService industryServiceService) {
        this.industryServiceService = industryServiceService;
    }

    @RequestMapping(value = "/search", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView search(@PathVariable("industryServiceId") Long industryServiceId, IndustryServiceSearchForm industryServiceSearchForm) {

        Page page = new Page(pageSize, industryServiceSearchForm.getPageNo());

        ModelAndView modelAndView = new ModelAndView("management/industryservice/industryServiceInfoCategorySearch");
        modelAndView.addObject("industryServiceId", industryServiceId);
        return modelAndView.addObject(industryServiceService.searchCategory(industryServiceSearchForm.getTitle(), industryServiceId, page));
    }

    @RequestMapping(value = "/search", method = RequestMethod.DELETE)
    public String delete(@PathVariable("industryServiceId") Long industryServiceId, @RequestParam("industryServiceInfoCategoryIds") Long[] industryIds) {
        industryServiceService.deleteAll(IndustryServiceInfoCategory.class, industryIds);
        return "redirect:/management/industryservice/"+industryServiceId+"/categories/search";
    }

    @ModelAttribute("industryServiceInfoCategory")
    public IndustryServiceInfoCategory populateModel(HttpServletRequest request) {
        String strId = request.getParameter("id");

        if (strId == null || strId.equals("-1")) {
            return new IndustryServiceInfoCategory();
        }
        return industryServiceService.getByCategoryId(new Long(strId));
    }

    @RequestMapping(value = "/{industryCategoryId}", method = RequestMethod.GET)
    public String edit(@PathVariable("industryServiceId") Long industryServiceId, @PathVariable Long industryCategoryId, Model model) {
        IndustryServiceInfoCategory industryCategory = industryServiceService.getByCategoryId(industryCategoryId);
        model.addAttribute("industryServiceId", industryServiceId);
        model.addAttribute(industryCategory);
        return "management/industryservice/industryServiceInfoCategoryForm";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String create(@PathVariable("industryServiceId") Long industryServiceId, Model model) {
        model.addAttribute(new IndustryServiceInfoCategory());
        model.addAttribute("industryServiceId", industryServiceId);
        return "management/industryservice/industryServiceInfoCategoryForm";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String save(@PathVariable("industryServiceId") Long industryServiceId, @ModelAttribute @Valid IndustryServiceInfoCategory industryCategory, BindingResult result) throws IOException {

        if (result.hasErrors()) {
            return "management/industryservice/industryServiceInfoCategoryForm";
        }
        industryCategory.setIndustryService(new IndustryService(industryServiceId));
        industryServiceService.saveOrUpdateCategory(industryCategory);

        return "redirect:/management/industryservice/"+industryServiceId+"/categories/search";
    }
}

